using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using MyApi.Entity;

namespace MyApi.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class UsersController : ControllerBase
    {
        // 定义一个泛型集合
        private IEnumerable<Users> GetUsers()
        {
            var users =
                new List<Users> {
                    new Users { Id = 1, Name = "黄炎榕", Password = "123456" },
                    new Users { Id = 2, Name = "曾婷", Password = "123456" },
                    new Users { Id = 3, Name = "张三", Password = "123456" }
                };
            return users;
        }

        // 无参查询
        [HttpGet]
        public dynamic Get()
        {
            var users = GetUsers();
            return users;
        }

        // 有参查询
        [HttpGet("{id}")]
        public dynamic Get(int id)
        {
            var users = GetUsers();
            var user = users.Where(x => x.Id == id).FirstOrDefault();
            return user;
        }

        // 增加
        [HttpPost]
        public dynamic Post(dynamic model)
        {
            return new
            {
                Code = 200,
                Data = model,
                Msg = string.Format("当您看到这条消息时，说明数据已经插入成功了！")
            };
        }

        // 修改
        [HttpPut("{id}")]
        public dynamic Put(int id, dynamic model)
        {
            return new
            {
                Code = 200,
                Data = model,
                Msg = string.Format("您修改的用户的Id为：{0}，看到这条消息说明已经修改成功了！", id)
            };
        }

        // 删除指定用户
        [HttpDelete("{id}")]
        public dynamic Delete(int id)
        {
            return new
            {
                Code = 200,
                Msg = string.Format("您删除的用户的Id为：{0}，看到这条消息说明已经删除成功了！", id)
            };
        }
    }
}
